import request from '@/utils/request'
import type { ApiResponse, LoginRequest, LoginResponse, User, UserVO } from '@/types/api'

// 用户登录
export const login = (data: LoginRequest): Promise<ApiResponse<LoginResponse>> => {
  return request({
    url: '/user/login',
    method: 'post',
    data
  })
}

// 用户注册
export const register = (data: {
  username: string
  password: string
  email?: string
}): Promise<ApiResponse<User>> => {
  return request({
    url: '/user/register',
    method: 'post',
    data
  })
}

// 获取用户信息
export const getUserInfo = (): Promise<ApiResponse<UserVO>> => {
  return request({
    url: '/user/getloginuser',
    method: 'get'
  })
}

// 用户登出
export const logout = (): Promise<ApiResponse<null>> => {
  return request({
    url: '/user/logout',
    method: 'post'
  })
}

// 修改密码
export const changePassword = (data: {
  oldPassword: string
  newPassword: string
}): Promise<ApiResponse<null>> => {
  return request({
    url: '/user/change-password',
    method: 'post',
    data
  })
}